package com.tjec.project.workflow.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.service.IBmsBusinessService;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocCatalogService;
import com.tjec.project.projectWorkbench.domain.BmsAgencyNo;
import com.tjec.project.projectWorkbench.domain.BmsCostNo;
import com.tjec.project.projectWorkbench.domain.BmsReportNo;
import com.tjec.project.projectWorkbench.domain.CancleReportNoVo;
import com.tjec.project.projectWorkbench.service.IBmsAgencyNoService;
import com.tjec.project.projectWorkbench.service.IBmsCostNoService;
import com.tjec.project.projectWorkbench.service.IBmsReportNoService;
import com.tjec.project.system.mapper.SysUserRoleMapper;
import com.tjec.project.system.service.ISysUserService;
import com.tjec.project.workflow.domain.BmsWorkflow;
import com.tjec.project.workflow.domain.dto.WorkflowCheckTookPartInDto;
import com.tjec.project.workflow.domain.dto.WorkflowParamsDto;
import com.tjec.project.workflow.domain.vo.BmsAttachmentsVo;
import com.tjec.project.workflow.domain.vo.BmsCatalogsVo;
import com.tjec.project.workflow.domain.vo.BmsPersonnelDeptVo;
import com.tjec.project.workflow.domain.vo.BmsWorkflowVo;
import com.tjec.project.workflow.service.IBmsWorkflowService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 项目流程Controller
 * 
 * @author pangyongfeng
 * @date 2020-05-18
 */
@Api(value = "项目流程接口", tags = {"项目流程接口"})
@RestController
@RequestMapping("/workflow")
public class BmsWorkflowController extends BaseController
{
    @Autowired
    private IBmsWorkflowService bmsWorkflowService;
    
    @Autowired
    private IBmsReportNoService bmsReportNoService;
    
    @Autowired
    private IBmsAgencyNoService bmsAgencyNoService;
    
    @Autowired
    private IBmsCostNoService bmsCostNoService;
    
    @Autowired
	private TokenService tokenService;
    
    @Autowired
	private SysUserRoleMapper sysUserRoleMapper;
    
    @Autowired
    private IBmsBusinessService bmsBusinessService;
    
    @Autowired
    private ISysUserService userService;

    /**
     * 查询项目流程列表
     */
    @ApiOperation("项目流程查询")
    @GetMapping("/list")
    public TableDataInfo list(BmsWorkflow bmsWorkflow)
    {

    	if(bmsWorkflow!=null && bmsWorkflow.getProjectName()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getProjectName().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setProjectNames(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getCode()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getCode().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setCodes(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getProjectLeader()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getProjectLeader().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setProjectLeaders(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getBusinessName()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getBusinessName().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setBusinessNames(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getReportNo()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getReportNo().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setReportNos(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getOrigin()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getOrigin().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setOrigins(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getAuditorPerson()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getAuditorPerson().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setAuditorPersons(resultList);
        }
    	
    	if(bmsWorkflow!=null && bmsWorkflow.getExpertName()!=null) {
        	List<String> resultList= new ArrayList<>(Arrays.asList(bmsWorkflow.getExpertName().split(" ")));
        	resultList.removeIf(String::isEmpty);
        	bmsWorkflow.setExpertNames(resultList);
        }
        
        if (bmsWorkflow.getType() == null || "".equals(bmsWorkflow.getType())) {
			// 当前登陆人工号
			String jobNo = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getJobNo();
			// 当前登陆人id
			String userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
			
			Set<String> codes = sysUserRoleMapper.selectRoleCodeByUserId(userId);
			if(codes!=null && codes.size()>0 && codes.contains("workflow_search_manager")) {
				
			}else {
				bmsWorkflow.setStartPerson(jobNo);
				bmsWorkflow.setApprovePerson(jobNo);
			}
		}
        startPage();
        List<BmsWorkflow> list = bmsWorkflowService.selectBmsWorkflowList(bmsWorkflow);
        return getDataTable(list);
    }

    /**
     * 新增项目流程
     */
    @ApiOperation("新增项目流程")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addWorkflow")
    public AjaxResult add(@RequestBody BmsWorkflowVo bmsWorkflow)
    {
        return toAjax(bmsWorkflowService.insertBmsWorkflow(bmsWorkflow));
    }

    /**
     * 修改项目流程
     */
    @ApiOperation("修改项目流程")
    @Log(title = "项目流程", businessType = BusinessType.UPDATE)
    @PostMapping("/updateWorkflow")
    public AjaxResult edit(@RequestBody BmsWorkflowVo bmsWorkflow)
    {
        return toAjax(bmsWorkflowService.updateBmsWorkflow(bmsWorkflow));
    }
    
    /**
     * 获取传入bpm系统的参数
     */
    @ApiOperation("获取传入bpm系统的参数")
    @GetMapping("/getBpmParams")
    public AjaxResult getBpmParams(BmsWorkflow bmsWorkflow)
    {
    	/*if(StringUtils.isBlank(bmsWorkflow.getBusinessTypeId()) || "".equals(bmsWorkflow.getBusinessTypeId())) {
    		return AjaxResult.error("业务类别id不能为空！");
    	}else if(StringUtils.isBlank(bmsWorkflow.getBusinessId()) || "".equals(bmsWorkflow.getBusinessId())) {
    		return AjaxResult.error("业务id不能为空！");
    	}else */if(StringUtils.isBlank(bmsWorkflow.getProjectId()) || "".equals(bmsWorkflow.getProjectId())) {
    		return AjaxResult.error("项目id不能为空！");
    	}
        return AjaxResult.success(bmsWorkflowService.getBpmParams(bmsWorkflow));
    }
    
    /**
     * 创建项目文档目录
     */
    @ApiOperation("创建项目文档目录")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/createCatalog")
    public AjaxResult createCatalog(@RequestBody BmsCatalogsVo bmsCatalogsVo) {
    	String res = bmsWorkflowService.createCatalog(bmsCatalogsVo);
    	if(!StringUtils.isBlank(res)) {
    		return AjaxResult.success("创建目录成功", res);
    	}else {
    		return AjaxResult.error("创建目录失败", null);
    	}
    }
    
    /**
     * 添加附件
     */
    @ApiOperation("添加附件")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/createAttachments")
    public AjaxResult insertAttachments(@RequestBody BmsAttachmentsVo bmsAttachmentsVo) {
    	int res = bmsWorkflowService.insertAttachments(bmsAttachmentsVo);
    	if(res>0) {
    		return AjaxResult.success("附件处理成功");
    	}else {
    		return AjaxResult.error("附件处理失败");
    	}
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增报告号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addReportNo")
    public AjaxResult addReportNo(BmsReportNo bmsReportNo)
    {
        return AjaxResult.success(bmsReportNoService.insertBmsReportNo(bmsReportNo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增招标代理号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addAgencyNo")
    public AjaxResult addAgencyNo(BmsAgencyNo bmsAgencyNo)
    {
        return  AjaxResult.success(bmsAgencyNoService.insertBmsAgencyNo(bmsAgencyNo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增造价报告号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addCostNo")
    public AjaxResult addCostNo(BmsCostNo bmsCostNo)
    {
        return  AjaxResult.success(bmsCostNoService.insertBmsCostNo(bmsCostNo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("删除报告号和招标代理号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/cancleReportNo")
    public AjaxResult cancleReportNo(CancleReportNoVo cancleReportNoVo)
    {
        return  AjaxResult.success(bmsWorkflowService.cancleReportNo(cancleReportNoVo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增报告号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addReportNoNew")
    public AjaxResult addReportNoNew(@RequestBody BmsReportNo bmsReportNo)
    {
        return AjaxResult.success(bmsReportNoService.insertBmsReportNo(bmsReportNo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增招标代理号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addAgencyNoNew")
    public AjaxResult addAgencyNoNew(@RequestBody BmsAgencyNo bmsAgencyNo)
    {
        return  AjaxResult.success(bmsAgencyNoService.insertBmsAgencyNo(bmsAgencyNo));
    }
    
    /**
     * 新增项目流程
     */
    @ApiOperation("新增造价报告号")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @PostMapping("/addCostNoNew")
    public AjaxResult addCostNoNew(@RequestBody BmsCostNo bmsCostNo)
    {
        return  AjaxResult.success(bmsCostNoService.insertBmsCostNo(bmsCostNo));
    }
    
    /**
     * 业务迁移
     */
    @ApiOperation("业务迁移")
    @PostMapping(value = "/migration")
    public AjaxResult migration(BmsBusiness bmsBusiness)
    {
    	return toAjax(bmsBusinessService.migrationBmsBusiness(bmsBusiness));
    }
    
    /**
     * 业务迁移
     */
    @ApiOperation("修改业务信息")
    @PostMapping(value = "/updateBusinessInfo")
    public AjaxResult updateBusinessInfo(BmsBusiness bmsBusiness)
    {
    	return AjaxResult.success(bmsBusinessService.updateBmsBusinessWorkflow(bmsBusiness));
    }
    
    /**
     * 业务迁移
     */
    @ApiOperation("修改书面归档状态")
    @PostMapping(value = "/updateWrittenStatus")
    public AjaxResult updateWrittenStatus(BmsBusiness bmsBusiness)
    {
    	return AjaxResult.success(bmsBusinessService.updateWrittenStatus(bmsBusiness));
    }
    
    /**
     * 业务迁移
     */
    @ApiOperation("修改业务迁移状态")
    @PostMapping(value = "/updateMoveStatus")
    public AjaxResult updateMoveStatus(BmsBusiness bmsBusiness)
    {
    	return AjaxResult.success(bmsBusinessService.updateMoveStatus(bmsBusiness));
    }
    
    @ApiOperation("根据老部门修改人事主管部门")
    @PostMapping(value = "/updatePersonnelDept")
    public AjaxResult updatePersonnelDept(@RequestBody BmsPersonnelDeptVo bmsPersonnelDeptVo) {
    	return AjaxResult.success(userService.updatePersonnelDept(bmsPersonnelDeptVo.getNewPersonnelDept(), bmsPersonnelDeptVo.getOldPersonnelDept()));
    };
    
    @ApiOperation("根据工号修改人事主管部门")
    @PostMapping(value = "/updatePersonnelDeptByJobNo")
    public AjaxResult updatePersonnelDeptByJobNo(@RequestBody BmsPersonnelDeptVo bmsPersonnelDeptVo) {
    	return AjaxResult.success(userService.updatePersonnelDeptByJobNo(bmsPersonnelDeptVo.getNewPersonnelDept(), bmsPersonnelDeptVo.getJobNo()));
    };
    
    /**
     * 检查用户是否参与过此流程
     */
    @ApiOperation("检查用户是否参与过此流程")
    @GetMapping("/checkTookPartIn")
    public AjaxResult checkTookPartIn(WorkflowCheckTookPartInDto workflowCheckTookPartInDto)
    {
        return AjaxResult.success(bmsWorkflowService.checkTookPartIn(workflowCheckTookPartInDto.getJobNo(),workflowCheckTookPartInDto.getInstanceId()));
    }
    
    /**
     * 业务迁移
     */
    @ApiOperation("修改业务状态-成果校审")
    @PostMapping(value = "/updateBmsBusinessStatus")
    public AjaxResult updateBmsBusinessStatus(BmsBusiness bmsBusiness)
    {
    	return AjaxResult.success(bmsWorkflowService.updateBmsBusinessStatus(bmsBusiness));
    }
    
}
